/**
 * 付款界面
 * author:zhangjlt
 * date:2017年3月16日13:30:42
 */
;
define(function(require, exports, module) {
	var wechat = require("wechat");
	var setPwd = {
		wxUserInfoObj: {},
		type: "type0",
		paypassword: "",
		paypassword2: "",
		sessionid: "", //验证码的id
		/**
		 * bindEvent 页面事件
		 */
		bindEvent: function() {
			//返回
			$("#return_setPwd").unbind().on("click", function() {
				//				window.location.href = "#/index/my";		

				window.history.back(-1);
			});
			$("#checkcode_setPwd_btn").unbind().on("click", function() {
				setPwd.verifyCheckCode();
			});

		},
		/**
		 * 进行页面路由监听配置
		 * 
		 */
		bindRout: function() {
			//公共页面监听路由
			Path.map("#home").to(function() {
				//返回首页
				common.loadPage({
					pageUrl: "html/index.html",
					reload: false,
					from: "left"
				});
			});
			//公共页面监听路由
			Path.map("#/index/:page_name").to(function() {
				console.log(this.params["page_name"]);
				var page_name = this.params["page_name"];
				//来自状态
				var from = "right";
				if(page_name == "my") {
					from = "left";
				}
				//记录状态
				common.loadPage({
					pageUrl: "html/" + page_name + ".html",
					reload: true,
					from: from
				});
			});
			Path.listen();
		},
		/**
		 * 页面数据加载
		 */
		initData: function() {
			var wxUserInfostr = window.localStorage.getItem("WxUserInfo");
			if(wxUserInfostr) {
				var wxUserInfoObj = JSON.parse(wxUserInfostr);
				setPwd.wxUserInfoObj = wxUserInfoObj;
				var loadType = window.localStorage.getItem("loadType");
				if(loadType && loadType == "type3") {
					setPwd.type = loadType;
					setPwd.setPageType();
					setPwd.initType3View();
				} else if(loadType && loadType == "type1") {
					//设置密码--type1
					setPwd.type = "type1";
					setPwd.setPageType();
					setPwd.initType1View();
				} else {
					if(wxUserInfoObj.status == "0") {
						var mobile = wxUserInfoObj.data.mobile;
						if(mobile && mobile.length == 11) {
							//设置密码--验证短信type0
							setPwd.type = "type0";
							setPwd.setPageType();
							setPwd.initCheckCodeView();
						} else {
							//设置密码--type1
							setPwd.type = "type1";
							setPwd.setPageType();
							setPwd.initType1View();
						}
					}
				}
			}
		},
		//设置界面显示类型
		setPageType: function() {
			var type = setPwd.type;
			$(".type_setPwd").hide();
			$("." + type).show();
		},
		inputPwdFirst: function(str) {
			$(".pwd_detail").text("");
			setPwd.paypassword = str;
			for(var i = 0; i < str.length; i++) {
				$("#Pwdtype1sp" + i).text("*");
			}
			if(str.length==6){
				$("#Pwdtype1btn").click();
			}
		},
		inputPwdSecond: function(str) {
			$(".pwd_detail").text("");
			setPwd.paypassword2 = str;
			for(var i = 0; i < str.length; i++) {
				$("#Pwdtype2sp" + i).text("*");
			}
			if(str.length==6){
				$("#Pwdtype2btn").click();
			}
		},
		inputPwdThird: function(str) {
			$(".pwd_detail").text("");
			//			setPwd.paypassword2 = str;
			for(var i = 0; i < str.length; i++) {
				$("#Pwdtype3sp" + i).text("*");
			}
			if(str.length==6){
				$("#Pwdtype3btn").click();
			}
		},
		//初始化type1界面
		initType1View: function() {
			$("#hideinput").focus();
			$(".type1").unbind().on("click", function() {
				$("#hideinput").focus();
			});
			$("#hideinput").unbind().on("input", function() {
				setPwd.inputPwdFirst($("#hideinput").val());
			});
			$("#Pwdtype1btn").unbind().on("click", function() {
				if(setPwd.paypassword && setPwd.paypassword.length == 6) {
					setPwd.type = "type2";
					setPwd.setPageType();
					setPwd.initType2View();
				} else {
					common.showTip({
						content: "请输入六位数字密码！"
					});
				}
			});
		},
		initType2View: function() {
			$("#hideinput_type2").focus();
			$(".type2").unbind().on("click", function() {
				$("#hideinput_type2").focus();
			});
			$("#hideinput_type2").unbind().on("input", function() {
				setPwd.inputPwdSecond($("#hideinput_type2").val());
			});
			$("#Pwdtype2btn").unbind().on("click", function() {
				if(setPwd.paypassword2 && setPwd.paypassword2.length == 6) {
					if(setPwd.paypassword == setPwd.paypassword2) {
						//调用接口保存密码
						var param = {
							"openId": window.localStorage.getItem("openId"),
							"appId": window.localStorage.getItem("appId"),
							"payPassword": setPwd.paypassword
						};
						common.loadData({
							url: "/wxmember/service/setPayPassword",
							param: param,
							successCallBack: function(res) {
								if(res.status == "0") {
									setPwd.wxUserInfoObj.data.hasPayPwd = true;
									var wxUserInfostr = JSON.stringify(setPwd.wxUserInfoObj);
									window.localStorage.setItem("WxUserInfo", wxUserInfostr);
									window.history.back(-1);
								} else {
									common.showTip({
										content: "设置密码出错，请重新设置！"
									}); //获取验证码出错
									//下一步 设置密码type1
									$("#hideinput").val("");
									$("#hideinput_type2").val("");
									//下一步 设置密码type1
									setPwd.type = "type1";
									setPwd.setPageType();
									setPwd.initType1View();
								}
							}, //回调函数
							cache: false //标识是不加载全部
						});
						//						window.history.back(-1);
					} else {
						common.showTip({
							content: "两次输入密码不一致，请重新输入！"
						});
						$("#hideinput").val("");
						$("#hideinput_type2").val("");
						//下一步 设置密码type1
						setPwd.type = "type1";
						setPwd.setPageType();
						setPwd.initType1View();

					}
				} else {
					common.showTip({
						content: "请再次输入六位数字密码！"
					});
				}
			});
		},
		initType3View: function() {
			$("#hideinput_type3").focus();
			$(".type3").unbind().on("click", function() {
				$("#hideinput_type3").focus();
			});
			$("#hideinput_type3").unbind().on("input", function() {
				setPwd.inputPwdThird($("#hideinput_type3").val());
			});
			var verificaMode = setPwd.wxUserInfoObj.data.verificaMode;
			if(verificaMode == "1") {
				$("#fg_setPwd").show();
				$("#fg_setPwd2").hide();
			} else {
				$("#fg_setPwd").hide();
				$("#fg_setPwd2").show();
			}
			$("#fg_setPwd").unbind().on("click", function() {
				var mobile = setPwd.wxUserInfoObj.data.mobile;
				if(mobile && mobile.length == 11) {
					//忘记密码--验证短信type0
					setPwd.type = "type0";
					setPwd.setPageType();
					setPwd.initCheckCodeView();
				} else {
					window.location.href = "#/index/register";
				}

			});
			$("#Pwdtype3btn").unbind().on("click", function() {
				if($("#hideinput_type3").val() && $("#hideinput_type3").val().length == 6) {
					//验证密码				
					var param = {
						"openId": window.localStorage.getItem("openId"),
						"appId": window.localStorage.getItem("appId"),
						"payPassword": $("#hideinput_type3").val()
					};
					common.loadData({
						url: "/wxmember/service/verifyPayPassword",
						param: param,
						successCallBack: function(res) {
							if(res.status == "0") {
								setPwd.type = "type1";
								setPwd.setPageType();
								setPwd.initType1View();
							} else {
								common.showTip({
									content: "密码输入错误，请重新输入！"
								}); //密码验证错误出错
								$("#hideinput_type3").val("");
								$("#hideinput_type3").focus();
								$(".pwd_detail").text("");
							}
						}, //回调函数
						cache: false //标识是不加载全部
					});

				} else {
					common.showTip({
						content: "请输入六位数字密码！"
					});
				}
			});
		},
		//获取验证码
		initCheckCodeView: function() {
			$("#checkcode_setPwd").focus();
			var $btn = $('#getcode_setPwd');
			$btn.html('重新发送（60s）').removeClass('register_active');
			var count = 60;
			var ret = setInterval(function() {
				--count;
				if(count == 0) {
					$btn.html('获取验证码');
					$btn.addClass("register_active");
					clearInterval(ret);
				} else {
					$btn.html('重新发送（' + count + 's）');
				}
			}, 1000);
			var moblie = setPwd.wxUserInfoObj.data.mobile;
			$("#moblie_h").text(moblie.substring(0, 3));
			$("#moblie_b").text(moblie.substring(7, 11));
			var param = {
				"openId": window.localStorage.getItem("openId"),
				"appId": window.localStorage.getItem("appId"),
				"userPhone": moblie
			};
			common.loadData({
				url: "/wxmember/service/getCheckCode",
				param: param,
				successCallBack: function(res) {
					if(res.status == "0") {
						setPwd.sessionid = res.data.sessionId;
					} else {
						common.showTip({
							content: res.message
						}); //获取验证码出错
						$btn.html('获取验证码');
						$btn.addClass("register_active");
						clearInterval(ret);
					}
				}, //回调函数
				cache: false //标识是不加载全部
			});
		},
		//验证短信码
		verifyCheckCode: function() {
			var _this = this;
			var userPhone = setPwd.wxUserInfoObj.data.mobile;
			var param = {
				"openId": window.localStorage.getItem("openId"),
				"appId": window.localStorage.getItem("appId"),
				"userPhone": userPhone,
				"sessionId": setPwd.sessionid,
				"checkCode": $("#checkcode_setPwd").val()
			};
			common.loadData({
				url: "/wxmember/service/verifyCheckCode",
				param: param,
				successCallBack: function(res) {
					if(res.status == "0") {
						//下一步 设置密码type1
						setPwd.type = "type1";
						setPwd.setPageType();
						setPwd.initType1View();
					} else {
						common.showTip({
							content: res.message
						});
					}
				},
				cache: false //标识是不加载全部
			});
		},
		/**
		 * each page init mothed
		 */
		initialize: function() {
			if(common.isWeiXin()) {
				$(".setPwd_main").css("top", "0rem");

			}
			//动态加载数据
			this.initData();
			//事件绑定
			this.bindEvent();
			//路由绑定
			this.bindRout();
		}
	};

	module.exports = setPwd;
});